home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / asm_reference / amigadisk_hardware.doc < prev    next >
Text File  |  1995-09-01  |  36KB  |  1,092 lines

  1.  
  2.  
  3.  
  4. This file is a more specialised look at hardware disc access. Requests for a
  5. specialised DOC file covering this material have been fairly numerous now, so
  6. I'm going to add this to the DOC file list. As per usual, this file will con-
  7. tain material culled from the Amiga Hardware Reference Manual plus extra add-
  8. itions of my own where deemed necessary (given that the Amiga manual writers
  9. are all devotees of the terse school of writing, the additions are deemed to
  10. be necessary on a regular basis!).
  11.  
  12.  
  13. Contents:
  14.  
  15.  
  16. Hardware Disc Access:Introduction
  17. Hardware Disc Access:Hardware Organisation
  18. Hardware Disc Access:CIA Timers For Delays
  19. Hardware Disc Access:MFM And GCR Encoding
  20. Hardware Disc Access:Basic I/O
  21. Hardware Disc Access:Track Selection
  22. Hardware Disc Access:Hardware Bugs
  23. Hardware Disc Access:File Updates
  24.  
  25.  
  26. Hardware Disc Access:Introduction
  27.  
  28.  
  29.     There have been a number of requests (particularly among ACC members)
  30. for a DOC file on the subject of hardware disc access. Well, here it is. This
  31. is partly a reworking of the Hardware Reference Manual (considered a strong
  32. contender in some circles for the Plain English Society's gobbledegook award)
  33. and various snippets of information that have come my way. As usual, I'll try
  34. to make this file as simple to understand as possible given the subject mat-
  35. ter.
  36.  
  37.  
  38.     Why hardware disc access? Well, games writers form one group of pro-
  39. grammers who immediately come to mind when one thinks of people who require
  40. this sort of information, as they are ALWAYS looking for sneaky tricks with
  41. which they can bypass the OS and do their own thing. Whether it's custom boot
  42. code or finding an alternative low-level disc data organisation to cram more
  43. sound samples or graphics data into their creations, games programmers always
  44. 'hit the hardware' first and foremost.
  45.  
  46.  
  47.     Further down the list come system rpogrammers, who need hardware info
  48. in order to provide those operating system routines that allow the remainder
  49. of the programming community to write applications without having to reinvent
  50. the wheel many times over to perform tasks such as loading and saving files.
  51.  
  52.  
  53.     Then there's the purely curious. ACC members usually fall into this
  54. category, whether or not they fall into the previous two categories. This DOC
  55. file should please this latter group too.
  56.  
  57.  
  58.     Programmers who have previously been satisfied with the DOS library
  59. should take note of the following caveats:
  60.  
  61.  
  62.     1) ANY direct access to the hardware is GUARANTEED to
  63.        destroy Exec's multitasking integrity. If you want to
  64.        access hardware without compromising multitasking,
  65.        use the resources (disk.resource and misc.resource
  66.        etc, accessed in a like manner to devices);
  67.  
  68.     2) Direct access to the disc hardware is complicated by
  69.        the use of two chip sets (the CIAs and the custom
  70.        chips);
  71.  
  72.     3) Direct access to the disc hardware also requires a
  73.        knowledge of hardware-level disc formats (more on
  74.        this later). If you are not sure what MFM and GCR
  75.        formats are, then I suggest seeking out suitable
  76.        reference material before using the information in
  77.        this DOC file.
  78.  
  79.  
  80. These warnings given, it is now time to move on. But before I do, note also
  81. that this information applies to floppy disc controllers ONLY. Since I do not
  82. have a hard disc, and certainly do not have a spare hard disc to experiment
  83. with, I cannot give information on hard disc controllers (and in any case, it
  84. is highly unlikely that anyone reading this DOC file is then going to go away
  85. and write system software unless they're already employed by Commodore-Amiga,
  86. and in that case they'll have far more information to hand than here!).
  87.  
  88.  
  89. Hardware Disc Access:Hardware Organisation
  90.  
  91.  
  92. Having stated that the disc hardware uses two different chip sets, I shall
  93. now outline the disc hardware organisation.
  94.  
  95.  
  96.     The CIA chips (component number 8520) are used for drive selection &
  97. disc sensing. The custom chips (specifically, Paula) are used for drive con-
  98. trol (read/write mode), and to link areas of memory reserved by the program-
  99. mer for disc access to the disc system. Note before I continue that as is the
  100. case for ALL memory intended to be used in conjunction with the custom chips,
  101. disc read/write buffers MUST be in CHIP RAM. This normally lies at the add-
  102. resses given below:
  103.  
  104.  
  105.         $00000-$7FFFF        : A500/A1000/A2000 with
  106.                       original chip set
  107.  
  108.         $00000-$FFFFF        : A500/A1000/A2000/A1500
  109.                       with Fatter Agnus chip
  110.                       and the new A500P
  111.  
  112.         $00000-$1FFFFF        : A3000 with Super Fat
  113.                       Agnus Chip (for rich
  114.                       programmers only!)
  115.  
  116.  
  117. However, this may NOT be true in the future! Commodore has already made four
  118. changes to the chip set (the latest being the Enhanced Chip Set specifica-
  119. tion as found on latest A3000s) and future changes may see future Amigas with
  120. relocatable CHIP RAM pages, so you have been warned! For a method of reser-
  121. ving CHIP RAM that works on ALL machines, use the Exec AllocMem() call via
  122. exec.library (this HAS to work or else Commodore will face lawsuits!).
  123.  
  124.  
  125.     Having dealt with CHIP RAM, now to the registers of the disc hardware
  126. system. First, the CIA registers. These lie at addresses $BFD000-$BFEFFF, but
  127. not all of these addresses are used. Below is a table outlining the CIA regi-
  128. ster set used by the disc hardware, including addresses, register names, and
  129. functions. NOTE:many signals accepted by the CIA registers are ACTIVE LOW, in
  130. other words, to turn the function ON requires a ZERO to be written into the
  131. approriate register or register bit (as opposed to the normal signal logic,
  132. called ACTIVE HIGH). In the table below, (L) denotes ACTIVE LOW, and (H) de-
  133. notes ACTIVE HIGH.
  134.  
  135.  
  136.     Register    Bit    Function
  137.     --------    ---    --------
  138.  
  139.     CIAAPRA         5    DSKRDY signal (L).
  140.     ($BFE001)
  141.                 Drive will pull this signal low (clear
  142.                 the bit) when the drive motor is known
  143.                 to be rotating at full speed (see Note 1
  144.                 below). 
  145.  
  146.  
  147.              4    DSKTRACK0 signal (L).
  148.  
  149.                 Drive will pull this signal low (clear
  150.                 the bit) when the disc drive read/write
  151.                 heads are positioned over track zero (see
  152.                 Note 2 below).
  153.  
  154.                 When this signal is active, software MUST
  155.                 NOT ATTEMPT TO STEP THE HEADS OUTWARDS!
  156.                 Modern add-on drives ignore the signal in
  157.                 these circumstances, but older drives may
  158.                 try to step out, resulting in a £35 repair
  159.                 bill for a dead drive! YOU HAVE BEEN WARNED!
  160.  
  161.  
  162.              3    DSKPROT signal (L).
  163.  
  164.                 Disc write protected signal:
  165.  
  166.                 0 = Disc write protected
  167.  
  168.                 1 = Disc writeable
  169.  
  170.  
  171.              2    DSKCHANGE (L).
  172.  
  173.                 Disc changed signal. This bit becomes
  174.                 zero when a disc is removed from the
  175.                 drive (signalling a disc change). The
  176.                 signal only becomes 1 again if a disc
  177.                 is inserted AND a step pulse is re-
  178.                 ceived (built-in safety feature, which
  179.                 makes sure that a real disc is being
  180.                 accessed, and not thin air).
  181.  
  182.  
  183.     CIABPRB         7    DSKMOTOR (L).
  184.     ($BFD000)
  185.                 Disc motor control signal. Implemented
  186.                 in a non-standard fashion. Drives LATCH
  187.                 the signal (i.e., keep their own private
  188.                 copy of it) when selected. So, to force
  189.                 a drive motor to be in a given state,
  190.                 set or clear this bit, THEN send a signal
  191.                 along one of the drive select lines. The
  192.                 drive will "remember" the state of its
  193.                 motor once the select signal is turned
  194.                 off.
  195.  
  196.                 0 = Motor ON
  197.  
  198.                 1 = Motor OFF
  199.  
  200.                 CAUTION:after turning the motor ON,
  201.                 software MUST wait for either 1/2 sec
  202.                 (500 ms) or DSKRDY to go low. Other-
  203.                 wise, the motor will NOT be at full
  204.                 speed, and a write operation will
  205.                 trash the disc!
  206.  
  207.  
  208.              6    DSKSEL3 (L).
  209.  
  210.                 Select drive 3 (DF3: if available on
  211.                 the user's system).
  212.  
  213.                 0 = Select drive for operation
  214.  
  215.                 1 = Drive ignores other signals
  216.  
  217.  
  218.              5    DSKSEL2 (L).
  219.  
  220.                 Select drive 2 (DF2: if available on
  221.                 the user's system).
  222.  
  223.                 0 = Select drive for operation
  224.  
  225.                 1 = Drive ignores other signals
  226.  
  227.  
  228.              4    DSKSEL1 (L).
  229.  
  230.                 Select drive 1 (DF1: if available on
  231.                 the user's system).
  232.  
  233.                 0 = Select drive for operation
  234.  
  235.                 1 = Drive ignores other signals
  236.  
  237.  
  238.              3    DSKSEL0 (L).
  239.  
  240.                 Select drive 0 (DF0:), THE internal drive
  241.                 on A500s (some A1000/2000 have two inter-
  242.                 nal drives), and the one used for booting
  243.                 WorkBench under KickStart 1.2/1.3.
  244.  
  245.                 0 = Select drive for operation
  246.  
  247.                 1 = Drive ignores other signals
  248.  
  249.  
  250.              2    DSKSIDE
  251.  
  252.                 Specify which disc read/write head to use.
  253.                 All Amiga drives are double sided. Hence
  254.                 there are two read-write heads. Use:
  255.  
  256.                 0 = Select UPPER surface disc head
  257.  
  258.                 1 = Select LOWER surface disc head
  259.  
  260.                 This value MUST be stable for at least
  261.                 100 microseconds before writing, and
  262.                 at least 1.3 MILLIseconds must elapse
  263.                 before switching DSKSIDE.
  264.  
  265.  
  266.              1    DSKDIREC
  267.  
  268.                 Specify direction to seek the heads.
  269.  
  270.                 0 = Seek TOWARD CENTRE SPINDLE
  271.  
  272.                 1 = Seek TOWARD OUTER EDGE
  273.  
  274.                 This signal MUST be set up BEFORE
  275.                 issuing a disc step signal. DO NOT
  276.                 use a single write to the register
  277.                 to perform both tasks! Use (for
  278.                 example):
  279.  
  280.                     MOVE.B #$0A,CIABPRB
  281.  
  282.                     ... short delay, e.g. NOP
  283.  
  284.                     MOVE.B #$09,CIABPRB
  285.  
  286.                 to step the heads outward on DF0:.
  287.  
  288.  
  289.              0    DSKSTEP (L).
  290.  
  291.                 Step the heads of the disc a distance
  292.                 of one track. So, to step 5 tracks,
  293.                 this signal needs to be sent 5 times.
  294.  
  295.                 The signal MUST be sent as:
  296.  
  297.                 Write 1 into bit
  298.                 Write 0 into bit
  299.                 Very short delay, e.g. a NOP
  300.                 Write 1 into bit
  301.  
  302.                 As in:
  303.  
  304.                     MOVE.B    #$08,CIABPRB
  305.                     MOVE.B    #$09,CIABPRB
  306.                     NOP
  307.                     MOVE.B    #$08,CIABPRB
  308.  
  309.                 for EACH INDIVIDUAL step operation.
  310.  
  311.                 See notes 3,4.
  312.  
  313.  
  314.     CIABICR         4    DSKINDEX (L).
  315.     ($BFDD00)
  316.                 Disc index pulse. Can be used to gen-
  317.                 erate a level 6 interrupt.
  318.  
  319.  
  320.             Notes on Above Table
  321.  
  322.     1) DSKRDY valid ONLY when drive motor is ON. At other times,
  323.        configuration information may obscure the meaning of this
  324.        input.
  325.  
  326.     2) Track zero is the OUTERMOST track of a disc. The usual
  327.        largest track is track 79, located nearest the centre of
  328.        the disc. Some drives (BUT NOT ALL!) are capable of
  329.        reading and writing up to track 83. DO NOT ASSUME that
  330.        software attempting such access will work on all machines!
  331.  
  332.     3) Amiga drives are guaranteed to step to the next track within
  333.        3 milliseconds (3 ms). Some drives will perform much faster
  334.        than this, but NOT ALL. Using software delay loops (decre-
  335.        menting a counter) is NOT ACCEPTABLE (and not portable to
  336.        machines using 68020/68030 processors with higher clock
  337.        speeds). A better method is to use the CIA timers to pro-
  338.        vide the delay. See later.
  339.  
  340.     4) When reversing directions, a MINIMUM delay of 18 ms is
  341.        required from the last step pulse. Settle time for Amiga
  342.        drives is specified at 15 ms.
  343.  
  344.  
  345. That covers the CIA portion of the disc hardware. Now I present a table of
  346. registers, bits and functions, this time for the various custom chip regis-
  347. ters used by the disc hardware system (located at $DFF000 onwards). Note:
  348. many registers exist as pairs, one for reading, one for writing. Write-only
  349. register are denoted by (W), read-only registers by (R).
  350.  
  351.  
  352.     Register    Bits    Function
  353.     --------    ----    --------
  354.  
  355.     $DFF020         3-0    DSKPTH (W)
  356.     $DFF022        15-0    DSKPTL (W)
  357.  
  358.                 Disc data pointer registers
  359.  
  360.                 Contains a pointer to the area of CHIP RAM
  361.                 used for data transfer. Store the address
  362.                 in these two registers. A single MOVE.L
  363.                 can be used for this.
  364.  
  365.  
  366.     $DFF024            DSKLEN (W)
  367.  
  368.                 Disc data length & access register
  369.  
  370.  
  371.              15    DMAEN
  372.  
  373.                 Secondary disc DMA enable. Disc access
  374.                 using a DMA channel CANNOT take place if
  375.                 this bit is zero!
  376.  
  377.  
  378.              14    WRITE
  379.  
  380.                 0 = Read access
  381.  
  382.                 1 = Write access
  383.  
  384.  
  385.             13-0    LENGTH
  386.  
  387.                 These bits contain the number of WORDS to
  388.                 transfer.
  389.  
  390.  
  391.     $DFF01A            DSKBYTR (R)
  392.  
  393.                 Disc data byte & status read
  394.  
  395.  
  396.              15    DSKBYT
  397.  
  398.                 Set to 1 by disc system when bits 0-7
  399.                 of this register contain a valid byte
  400.                 of data. When this register is read by
  401.                 the 68000 or DMA, this bit is cleared
  402.                 for the next data byte status.
  403.  
  404.  
  405.              14    DMAON
  406.  
  407.                 Indicates when DMA is ACTUALLY ON. Is
  408.                 only set to 1 when ALL DMA enable bits
  409.                 pertinent to the disc system are set!
  410.  
  411.  
  412.              13    DISKWRITE
  413.  
  414.                 Reflects the state of the WRITE bit in
  415.                 DSKLEN above.
  416.  
  417.  
  418.              12    WORDEQUAL
  419.  
  420.                 Indicates that the DSKSYNC register
  421.                 equals the disc input stream. This
  422.                 bit is only true while the input
  423.                 stream matches the sync register,
  424.                 which can be for as little as 2
  425.                 microseconds! See below for DSKSYNC.
  426.  
  427.  
  428.             11-8    Currently unused. DO NOT RELY ON READ
  429.                 VALUE.
  430.  
  431.  
  432.              7-0    DATA
  433.  
  434.                 Value of disc data byte that has been
  435.                 read.
  436.  
  437.  
  438.     $DFF07E            DSKSYNC (W)
  439.  
  440.                 Disc synchronisation value register
  441.  
  442.  
  443.             15-0    Disc synchronisation value. Normally
  444.                 set to the magic MFM sync mark value
  445.                 of $4489 by AmigaDOS. Hardware pro-
  446.                 grammers can experiment with other
  447.                 values (CAUTION!).
  448.  
  449.  
  450.     $DFF010            ADKCONR (R)
  451.     $DFF09E            ADKCON     (W)
  452.  
  453.                 Audio and Disc Control Register
  454.                 Use in the following manner:
  455.  
  456.                 MOVE.W    ADKCONR,D0 for READING
  457.                 MOVE.W    D0,ADKCON  for WRITING
  458.  
  459.  
  460.              15    SET/CLR
  461.  
  462.                 Best illustrated by an example. If
  463.                 the word $8001 is written to a regi-
  464.                 ster with a SET/CLR bit, then bit 0
  465.                 will be SET, and all other bits left
  466.                 untouched. If the word $0001 is writ-
  467.                 ten, then bit 0 will be CLEARED, and
  468.                 all other bits left untouched. This
  469.                 allows the programmer to affect ONLY
  470.                 those bits required for the given
  471.                 operation. Also, see typed_hardware.doc
  472.                 for more info.
  473.  
  474.  
  475.              14    PRECOMP1
  476.              13    PRECOMP0
  477.  
  478.                 Precompensation value. Values are:
  479.  
  480.                 00 : No precompensation
  481.                 01 : 140 ns precompensation
  482.                 10 : 280 ns precompensation
  483.                 11 : 560 ns precompensation
  484.  
  485.  
  486.              12    MFMPREC
  487.  
  488.                 0 = GCR precompensation (allows
  489.                     Amiga drives to read Apple
  490.                     Macintosh discs with certain
  491.                     programming tricks)
  492.  
  493.                 1 = MFM (Normal AmigaDOS setting,
  494.                     also allows Atari ST/IBM PC
  495.                     discs to be read)
  496.  
  497.  
  498.              10    WORDSYNC
  499.  
  500.                 Set to 1 to allow synchronising and
  501.                 starting of DMA on disc read of a word.
  502.                 DSKSYCN contains the synchronisation
  503.                 word. VERY USEFUL.
  504.  
  505.  
  506.               9    MSBSYNC
  507.  
  508.                 Set to 1 to enable synchronisation on
  509.                 most significant bit of input (used for
  510.                 GCR setting)
  511.  
  512.  
  513.               8    FAST
  514.  
  515.                 Value of 1 selects 2 microseconds per
  516.                 bit cell (MFM). Data must be valid raw
  517.                 MFM data. Value of 0 selects 4 micro-
  518.                 seconds per bit cell (GCR).
  519.  
  520.  
  521. This covers the register organisation of the disc hardware. Before covering
  522. how to use the registers for disc access, it is time to cover MFM encoding &
  523. related topics.
  524.  
  525.  
  526. Hardware Disc Access:CIA Timers For Delays
  527.  
  528.  
  529. The CIA chips contain on-board timer hardware, that allows the programmer to
  530. implement precisely timed delay loops that can migrate happily across differ-
  531. ent versions of the Amiga. This facility will be vital in managing the disc
  532. hardware, so I shall now describe the usage of the CIA timers to generate a
  533. fixed-period delay loop.
  534.  
  535.  
  536.     Now, the CIA timers are often chosen to implement delay loops that
  537. are relatively independent of the remaining Amiga hardware. However, accord-
  538. ing to the Concise Hardware Reference Manual, the timing signals for NTSC and
  539. PAL Amigas differs slightly. The timing signals are:
  540.  
  541.  
  542.         NTSC    :    715.909 KHz
  543.  
  544.         PAL    :    715.379 KHz
  545.  
  546.  
  547. However, these timing signals remain constant across different Amiga specifi-
  548. cations, from base A500 all the way to A3000 with 68040 accelerator board. As
  549. a means of creating processor-independent delay loops, the CIAs are the prime
  550. choice.
  551.  
  552.  
  553.     The best timer to use under most circumstances is the CIA-B Timer B,
  554. and this timer can be found at $BFD600/$BFD700. It is a 16-bit timer, and to
  555. use it requires setting control bits in the CIA control registers. But first
  556. let's see the sequence of events in action:
  557.  
  558.  
  559.             MOVE.W    #TIMEVAL,D0    ;Timer value
  560.  
  561.             MOVE.B    D0,CIABTBLO    ;set low byte
  562.             LSL.W    #8,D0
  563.             MOVE.B    D0,CIABTBHI    ;set high byte
  564.  
  565.  
  566. This sequence sets the CIA-B Timer B write only latches. These are registers
  567. that hold the timing value until the control register forces this value to be
  568. loaded into the actual timer counters and counting to begin. Having set the
  569. timer latches, it is time to start the timer. An instruction that can be used
  570. is:
  571.  
  572.  
  573.             MOVE.B    #%00011001,CIABCRB
  574.  
  575.  
  576. If one refers to the Concise Hardware Reference Manual, and examines the bit
  577. allocations of CIABCRB, the effect of the above instruction is:
  578.  
  579.  
  580.         1) Alarm bit (bit 7):don't care about it, so set
  581.            it to 0.
  582.  
  583.         2) Inmode bits (Bits 6,5):Want timer to count the
  584.            incoming clock signal pulses, so set these bits
  585.            to 00.
  586.  
  587.         3) Load bit (bit 4):Force timer to load the latch
  588.            values into the timer, and then count down to
  589.            zero, so set this bit to 1.
  590.  
  591.         4) Runmode bit (bit 3):Set to 1 for one-shot mode
  592.            (since we only want a single delay).
  593.  
  594.         5) Outmode bit (bit 2):Set to 0 to receive a pulse
  595.            on the line being sensed.
  596.  
  597.         6) PBON bit (bit 1):Set to 0 (leave PB7 line alone
  598.            as it's needed elsewhere!).
  599.  
  600.         7) Start bit (bit 0):Set to 1 to start the timer
  601.            counting down.
  602.  
  603.  
  604. OK, now we've set off our timer. Now we need to be able to sense when it has
  605. reaced the timeout condition (i.e., time is up). Well, the best way is to set
  606. up the interrupt control register (CIABICR) to allow the CIA to generate an
  607. interrupt upon a timeout. To allow Timer B to generate an interrupt within
  608. the CIA itself, use:
  609.  
  610.  
  611.             MOVE.B    #%10000010,CIABICR
  612.  
  613.  
  614. or similar. This writes the control bits into the CIA's interrupt latch, and
  615. allows Timer B to create an interrupt within the CIA.
  616.  
  617.  
  618.     Now, reading the same register address will read the CIA-B interrupt
  619. input bits, and if Bit 1 is set, then the timer has timed out. Of course, as
  620. CIA-B will generate a level 6 interrupt (IPL6 on the 68000), it is possible
  621. to write a level 6 interrupt handler to check for this, but it is also poss-
  622. ible to read the register directly (especially if the level 6 interrupt vec-
  623. tor is disabled or redirected by the programmer for other reasons).
  624.  
  625.  
  626.     Timer A on the CIA-B can also be used (as it is not in use for any
  627. operating system purposes) but if the operating system has been killed off by
  628. the programmer, ALL of the timers are free. The above code applies mainly if
  629. at least some facets of the operating system are being kept alive (for true
  630. friendliness within the multitasking environment, use the CIA Resource, and
  631. the Exec OpenResource() call).
  632.  
  633.  
  634.     Also, the programmer can choose to test the bits of the CIA interrupt
  635. register directly or write an interrupt handler to do it (INT2 if using the
  636. CIA-A timers, INT6 if using CIA-B). Don't forget to enable the required in-
  637. terrupt in the 4703 interrupt controller (custom chip-write to INTENA, at ad-
  638. dress $DFF09A to control the required interrupts), and to change the value of
  639. the 68000 status register if required by your code. This requires access to
  640. supervisor mode, which can be done in two ways. First, there is an Exec call
  641. that will run a piece of code in supervisor mode:
  642.  
  643.  
  644.             MOVE.L    ExecBase,A6
  645.             JSR    Superstate(A6)
  646.             MOVE.L    D0,Old_SSTk    ;keep this value!
  647.  
  648.  
  649. and allow recovery of user mode via:
  650.  
  651.  
  652.             MOVE.L    Old_SSTk,D0
  653.             MOVE.L    ExecBase,A6
  654.             JSR    UserState(A6)
  655.  
  656.  
  657.  
  658. or alternatively, if you're killing off the operating system, try this rather
  659. naughty piece of code:
  660.  
  661.  
  662.             LEA    MyTrap0(PC),A0
  663.             MOVE.L    A0,$80        ;addr of TRAP #0 vector
  664.             TRAP #0
  665.  
  666.             ...        ;from here on, MOVE SR is
  667.                     ;allowed as you are in
  668.                     ;supervisor mode!
  669.  
  670.             ...
  671.  
  672.     MyTrap0        MOVE.W    (SP)+,D0    ;get status reg
  673.             BSET    #13,D0        ;set supervisor mode
  674.             MOVE.W    D0,-(SP)    ;put back
  675.             RTE            ;return in supervisor mode!
  676.  
  677.  
  678. This is a very naughty piece of code (which destroys multitasking integrity)
  679. of the sort that a games coder might like to use when returning to the oper-
  680. ating system is not called for.
  681.  
  682.  
  683.     Now that we can implement CIA timer delay loops (needed later on), it
  684. is time to look at MFM and GCR data encoding.
  685.  
  686.  
  687. Hardware Disc Access:MFM And GCR Encoding
  688.  
  689.  
  690. Those who haven't encountered direct writing to disc before may be surprised
  691. to find that the actual bit patterns written to the disc differ from the bit
  692. patterns of the data that the programmer writes to disc when using AmigaDOS
  693. routines. Well, all is to be revealed.
  694.  
  695.  
  696.     The fundamental problem of writing data to a disc is detecting the
  697. difference between a zero bit and a one bit. The next problem is detecting
  698. the difference between individual bits, be they ones or zeros. Since the disc
  699. read/write head uses magnetic fields for its operations, there are only two
  700. possibilities:magnetic field on and magnetic field off. Allocating each of
  701. these signals to a zero and one bit respectively would render detection of an
  702. individual bit impossible.
  703.  
  704.  
  705.     To solve this problem, a scheme called frequency modulation was de-
  706. vised, whereby CHANGES in the magnetic field value written to the disc were
  707. used to signal the start of a bit, and the TYPE of change encountered used
  708. to determine which type of bit was written during the original write opera-
  709. tion. This scheme works for low-volume data storage, but when the density of
  710. the data exceeds a certain value, the frequency of read/write errors builds
  711. up to an unacceptable value. For the technically minded, the problem is that
  712. high-speed flux changes take time to generate the respective electronic sig-
  713. nal change, a time known as the half zero bit length, and the minimum value
  714. of this is the main determinant of the maximum density of data storable on a
  715. disc. Also, there is a delay during initiation of read/write operations, this
  716. value being the precompensation value (although Amiga disc drives are capable
  717. of handling read/write operations with zero precompensation). Generally, the
  718. faster the read/write operation, the more precompensation needed.
  719.  
  720.  
  721.     Overcoming the problem of large precompensation values for densely
  722. packed data on a disc led to the formation of Modified Frequency Modulated
  723. encoding (MFM). This encoding scheme expands the actual data to twice its
  724. original size, but in doing so generates raw data to write to the disc that
  725. is efficient enough to allow 880K of user data to be stored on a 3.5 inch
  726. disc.
  727.  
  728.  
  729.     So, what is MFM? The exact relationship is:
  730.  
  731.  
  732.         User Data Bit        MFM Bits
  733.         -------------        --------
  734.  
  735.             1          01
  736.  
  737.             0          10 if following a 0 data bit
  738.  
  739.             0          00 if following a 1 data bit
  740.  
  741.  
  742. Now according to the Amiga Hardware Reference Manual, it is possible to use
  743. the blitter to encode and decode the MFM data. Well, for the record, I have
  744. not tried this, so those willing to experiment might like to disassemble the
  745. trackdisk.device and see if Commodore have written blitter MFM routines (and
  746. if they haven't, why mention this?). Really clever programmers reading this
  747. can write their own.
  748.  
  749.  
  750.     GCR encoding is an entirely different matter. Sad to say, Commodore-
  751. Amiga do NOT give any in-depth information on this subject, other than to say
  752. that a translation table (called a nibbliser table) is used to prevent too
  753. many consecutive 1 or 0 bits occuring in the data stream. Users who want this
  754. information should obtain a comprehensive reference work covering hardware-
  755. level disc formats and floppy disc hardware in general. Anyone familiar with
  756. Apple II (there's nostalgia for you!) disc drives and their hardware-level
  757. format, or the Apple Macintosh disc format, please get in touch so that I can
  758. provide information on GCR encoding for Amiga users!
  759.  
  760.  
  761.     As I was perusing the subject, a piece of code that performed MFM en-
  762. coding and decoding without the need for the trackdisk device appeared on ACC
  763. disc 18, so I decided to have a further look. The code turned out to be quite
  764. interesting (as it had no comments explaining it-thanks Mark Flemans for an
  765. example of the minimalist approach to documentation!), but apparently works.
  766.  
  767.  
  768. Hardware Disc Access:Basic I/O
  769.  
  770.  
  771. Now is the time to start using the registers and performing some actual disc
  772. operations. The following code is all my own, to be considered freeware and
  773. rippable/editable to the reader's preferences/sexual leanings etc.
  774.  
  775.  
  776.     First, disc access via the DMA channels. This is the usual method, &
  777. has the advantage of allowing the 68000 to get on with other processing while
  778. a disc is being read. Now users with one disc drive will not normally need to
  779. consider the drive select signals, but I shall include the drive select code
  780. so that the code can be used by users with more than one drive. The sequence
  781. of operations looks like this in assembler:
  782.  
  783.  
  784.         MOVE.B    #$78,CIABPRB    ;switch ON motor, no drive select
  785.         MOVE.B    #$F0,CIABPRB    ;select drive 0
  786.  
  787. bwait1        BTST    #5,CIAAPRA    ;drive ready? (Busy wait-sigh)
  788.         BNE.S    bwait1        ;wait until so
  789.  
  790.         LEA    $DFF000,A5    ;point to custom chips
  791.  
  792.         MOVE.L    #BUFFER,DSKPTH(A5)    ;point to our buffer
  793.  
  794.  
  795. * The two statement sets below are commented out. Remove the comment
  796. * characters for the operation wanted.
  797.  
  798.  
  799. *        MOVE.W    #$4000,DSKLEN(A5)    ;turn off secondary disc DMA
  800. *        MOVE.W    #$8000,D0        ;secondary DMA on, READ DISC
  801. *        ADD.W    #DATASIZE,D0        ;no of WORDS to READ
  802. *        MOVE.W    D0,DSKLEN(A5)        ;need to write TWICE to
  803. *        MOVE.W    D0,DSKLEN(A5)        ;start the operation!
  804.  
  805.  
  806. *        MOVE.W    #$4000,DSKLEN(A5)    ;turn off secondary disc DMA
  807. *        MOVE.W    #$C000,D0        ;secondary DMA on, WRITE DISC
  808. *        ADD.W    #DATASIZE,D0        ;no of WORDS to WRITE
  809. *        MOVE.W    D0,DSKLEN(A5)        ;need to write TWICE to
  810. *        MOVE.W    D0,DSKLEN(A5)        ;start the operation!
  811.  
  812.         ... here wait for DMA to complete
  813.  
  814.         MOVE.W    #$4000,DSKLEN(A5)    ;turn off disc DMA again
  815.  
  816.         MOVE.B    #$F8,CIABPRB        ;turn motor OFF
  817.         MOVE.B    #$F0,CIABPRB        ;select drive 0
  818.  
  819.  
  820. Ok, some notes about the above code. First, the motor is switched on, with
  821. no drive select signals sent. Because the signals are ACTIVE LOW, the bits
  822. to turn OFF a function are SET. The same applies to the drive select signal
  823. immediately following.
  824.  
  825.  
  826.     Now anyone who has read my previous DOC files will know that I HATE
  827. busy waits, such as the one that I've sadly included here. Well, this code is
  828. intended to be simple to understand, so I'll break the golden rule and use a
  829. busy wait to wait for the drive to be ready. This just waits for the DSKRDY
  830. bit to go low. Of course, an alternative is to use the disk index pulse bit
  831. to trigger a level 6 interrupt, and then read DSKRDY within the interrupt,
  832. while your code does something more useful than twiddle its thumbs.
  833.  
  834.  
  835.     Now, point A5 at the custom chips, to make life easier (especially if
  836. you use the hardware.i ACC include file), then point the DSKPTH/L registers
  837. at the buffer of your choice.
  838.  
  839.  
  840.     Here, we decide whether to read or write to disc. Both code sections
  841. are included above. Just remove the comment markers from the one required, &
  842. delete the other.
  843.  
  844.  
  845.     Note that in order to set off a DMA disc operation, the DSKLEN regi-
  846. ster must be written to TWICE. This is intended to prevent accidental writes
  847. to a disc (which are of course always to be avoided!), and ensure that when
  848. a disc operation is requested, the hardware receives a genuine request. The
  849. above code does this.
  850.  
  851.  
  852.     Of course, it is possible to read or write the 'hard' way, using the
  853. 68000 to keep track of things, but the above is more efficient (barring the
  854. busy wait).
  855.  
  856.  
  857.     Now, some restrictions. First, the above should be used to read or
  858. write an entire track at a time. It is possible to modify it to read in one
  859. sector at a time, but the above is not intended to be an advanced routine.
  860. Sector-level disc I/O requires using the DSKBYTR and DSKSYNC registers, and
  861. also probably requires handling via interrupts not only for maximum effic-
  862. iency, but for maximum data integrity too. When using the above code for a
  863. track read or write, the size of the buffer should be:
  864.  
  865.  
  866.     (512 * 11)       *    2    + 2    = 11266 bytes
  867.  
  868.     Size of 1 track        MFM
  869.                  Overhead
  870.  
  871.  
  872. The reason for the size given is 1) a single sector is 512 bytes of user data
  873. on a standard AmigaDOS disc; 2) there are 11 sectors per track; 3) all data
  874. in MFM format is expanded to twice the uncoded size; 4) an extra word is to
  875. be reserved to take account of a hardware bug (see below) that Commodore just
  876. happen to have mentioned in the Amiga Hardware Reference Manual.
  877.  
  878.  
  879.     Now, there remains another task to be performed before reading data
  880. from a disc. That is to make sure that the data coming off the disc is syn-
  881. chronised to the actual data layout. The programmer wants to be sure that a
  882. data stream coming in actually STARTS at the first word of the first sector
  883. of the track being read. How is this done?
  884.  
  885.  
  886.     Well, for an AmigaDOS disc, load DSKSYNC with the magic value $4489.
  887. This is a value called a 'sync mark'. This value is chosen so that there is
  888. no way possible that any data on the disc encodes to this value. There are a
  889. number of such values, but AmigaDOS uses $4489. Normal user data will never
  890. encode to this number using MFM encoding as given above, and so the disc sys-
  891. tem can use this value to mark the start of the track.
  892.  
  893.  
  894.     Having loaded DSKSYNC with the sync mark value, enable the WORDSYNC
  895. bit in ADKCON prior to reading or writing. Then, data transfer will NOT be
  896. performed until the sync mark is found, at which point the following word of
  897. data on the disc will be the start point for data transfer. Use:
  898.  
  899.  
  900.         MOVE.W    #$4489,DSKSYNC(A5)
  901.  
  902.  
  903. where A5 points to the start of the custom chip registers ($DFF000). Then set
  904. WORDSYNC in ADKCON:
  905.  
  906.  
  907.         MOVE.W    #$9500,ADKCON(A5)
  908.  
  909.  
  910. which sets the WORDSYNC, MFMPREC and FAST bits. Do this BEFORE the main code
  911. for reading or writing discs above.
  912.  
  913.  
  914. Hardware Disc Access:Track Selection
  915.  
  916.  
  917. This is the final basic function to be performed. Track selection is a mite
  918. complicated, because the drives do not signal directly (except for track 0)
  919. when the heads are aligned over a given track. So, to perform this function,
  920. the programmer MUST first seek to track 0 (at which point the DSKTRACK0 bit
  921. of CIAAPRA will become zero. This requires first that the drive be selected,
  922. then the motor turned on, then DSKDIREC set to 1 to force a seek to the outer
  923. edge of the disc (where track 0 lies). Then, send a step pulse to DSKSTEP as
  924. outlined above until DSKTRACK0 goes low. Then, SAVE this track number in a
  925. variable set up within the program for this purpose. From this point on, to
  926. find a given track requires a seek in the given direction, incrementing the
  927. variable each time a step inwards is performed, and decrementing the varia-
  928. ble when a step outwards is performed. Locating a specified track then requ-
  929. ires a compare with the desired track number. For example, after selecting a
  930. drive and turning on the motor, code such as the following may be used:
  931.  
  932.  
  933. FindTRK0    btst    #4,CIAAPRA    ;hit track 0?
  934.         beq.s    FoundTRK0    ;skip if so
  935.  
  936.         move.b    #%11110010,CIABPRB    ;seek to outer, DF0:
  937.  
  938.         ...    ;here set up a delay using a CIA timer
  939.         ...    ;for 18 milliseconds
  940.  
  941.         move.b    #%11110001,CIABPRB    ;set DSKSTEP high
  942.         move.b    #%11110000,CIABPRB    ;then low
  943.         move.b    #%11110001,CIABPRB    ;then high again
  944.  
  945.         ...    ;here set up 3 ms delay using
  946.         ...    ;a CIA timer
  947.  
  948.         bra.s    FindTRK0        ;and check if hit track 0
  949.  
  950. FoundTRK0    clr.w    TrackNumber    ;absolute variable (sigh)
  951.  
  952.         ...            ;from here, can seek anywhere!
  953.  
  954.  
  955. Having used this, code to seek in a given direction can use a second varia-
  956. ble (call it TrackWanted). A CMP between the two will establish the seek di-
  957. rection. Alternatively, use:
  958.  
  959.  
  960.         move.w    TrackNumber,d0
  961.         move.w    TrackWanted,d1
  962.         sub.w    d0,d1        ;check seek direction
  963.         beq.s    Done        ;already there!
  964.         bpl.s    Seek_Inward
  965.  
  966.         neg.w    d1
  967.  
  968.         move.b    #%11110110,CIABPRB    ;set DSKDIREC outwards
  969.  
  970.         ...        ;here CIA timer delay, 18 ms
  971.  
  972.  
  973. Seek_Out    move.b    #%11110001,CIABPRB    ;set DSKSTEP high
  974.         move.b    #%11110000,CIABPRB    ;then low
  975.         move.b    #%11110001,CIABPRB    ;then high again
  976.  
  977.         subq.w    #1,TrackNumber        ;update track count
  978.         cmp.w    TrackWanted,TrackNumber    ;found req'd track?
  979.         beq.s    Seek_Done        ;got it!
  980.  
  981.         subq.w    #1,d1
  982.         bne.s    Seek_out
  983.  
  984.         ...
  985.  
  986. Seek_Inward    move.b    #%11110100,CIABPRB    ;set DSKDIREC inwards
  987.  
  988.         ...        ;here CIA timer delay, 18 ms
  989.  
  990.  
  991. Seek_In        move.b    #%11110001,CIABPRB    ;set DSKSTEP high
  992.         move.b    #%11110000,CIABPRB    ;then low
  993.         move.b    #%11110001,CIABPRB    ;then high again
  994.  
  995.         addq.w    #1,TrackNumber        ;update track count
  996.         cmp.w    TrackWanted,TrackNumber    ;found req'd track?
  997.         beq.s    Seek_Done        ;got it!
  998.  
  999.         subq.w    #1,d1
  1000.         bne.s    Seek_In
  1001.  
  1002. Seek_Done    ...
  1003.  
  1004.  
  1005. This should cover most needs for hardware disc programmers. Those requiring a
  1006. routine to perform sector-level access, be warned that the facilities provi-
  1007. ded for sector-level access are limited. First, the DSKBYTR register must be
  1008. polled on a regular basis while the track is being scanned. Second, since the
  1009. disc system does NOT maintain sector counters in hardware, the programmer has
  1010. to provide variables for this purpose within the program. Unless there is a
  1011. shortage of available memory, and sector-level access is the ONLY option open
  1012. to the programmer, then reading an entire track in one go is a far superior
  1013. option.
  1014.  
  1015.  
  1016. Hardware Disc Access:Hardware Bugs
  1017.  
  1018.  
  1019. Now the bad news. The Amiga Hardware Reference Manual contains a short para-
  1020. graph (tucked away where it can't be seen too easily so as not to alarm the
  1021. first-time hardware programmer!) which mentions a disc hardware bug. Well, it
  1022. is really two bugs, one occurring during a read operation, and one occurring
  1023. during a write operation.
  1024.  
  1025.  
  1026.     Firstly, during a read operation, the last word requested may not be
  1027. read. This doesn't always happen, but just in case it does, the buffer for a
  1028. hardware disc read should be one word longer than necessary. Also, for write
  1029. operations, the extra last word should match the first word of the buffer. I
  1030. forgot to reserve space for sync marks in the buffer information above (sorry
  1031. about that!), so make the first word the sync mark, then a track full of data
  1032. and finally a copy of the sync mark.
  1033.  
  1034.  
  1035. Hardware Disc Access:File Updates
  1036.  
  1037.  
  1038. For updated versions of this file, or any of the other files in this series
  1039. of DOC files, the contact addresses are:
  1040.  
  1041.  
  1042.         Dave Edwards        Mark Meany
  1043.         232 Hale Road        12 Hinkler Road
  1044.         WIDNES            Thornhill
  1045.         Cheshire        SOUTHAMPTON
  1046.         WA8 8QA            Hants
  1047.  
  1048.  
  1049. The first of these is mine. Sending a blank formatted disc to me at the above
  1050. address, with a stamped, self-addressed jiffy bag for return postage (stand-
  1051. ard 1st class stamp sufficient) will get you the files of your choice, writ-
  1052. ten to your disc in uncompressed ASCII format. Sending a disc to Mark at the
  1053. above address (plus jiffy bag, stamp etc.) will get you one of the ACC club
  1054. discs which contain these files spread over a period (they were intended for
  1055. the ACC club readership initially, and are now Public Domain DOCs distributed
  1056. as part of the ACC club material by Mark). All DOC files appearing on the ACC
  1057. Club discs are in compressed format, and you'll need the packer/depacker that
  1058. is supplied with the ACC discs to be able to edit them, if this is your wish.
  1059.  
  1060.     Should anyone out there have information to add to the files, please
  1061. contact either Mark or myself at the addresses above, sending your info as an
  1062. uncompressed ASCII file. I shall add the info to the relevant file when time
  1063. allows, and send off the latest version upon request. All contributors will
  1064. be acknowledged gratefully.
  1065.  
  1066.  
  1067.             **** VERY IMPORTANT ****
  1068.  
  1069.  
  1070. Since the DOC files have now grown alarmingly in number (and size-they occupy
  1071. nearly 3MB when decompressed!) from 1/10/91 onwards, ALL requests to me for a
  1072. copy of the DOC DISC should be accompanied by TWO blank discs for each set of
  1073. DOC DISCS required. So, if three sets of DOC DISCS are required, the reader
  1074. should now send SIX blank discs, for example. Until I can arrange with Mark
  1075. to have the latest updates sent to him, please send ALL requests for DOC DISC
  1076. copies directly to me until some time in December 1991 (by which time the ACC
  1077. should have the latest updates via Mark). To give the reader some idea of the
  1078. monumental size of the DOCS, the files occupy 900K AFTER compression, and at
  1079. the time of typing occupy 2,787,490 bytes when unpacked! So, this should tie
  1080. your reading time up for the next three years...
  1081.  
  1082.  
  1083. UPDATE    : typed_hardware_disc.doc current as of 14/2/92
  1084.       Contributors : Dave Edwards
  1085.              Mark Meany (ACC 17)
  1086.              Vandal of Killers (Snippets, ACC 17)
  1087.              Mark Flemans (ACC 18, movetrack00.s &
  1088.                     tcode.s
  1089.  
  1090.  
  1091.  
  1092.